﻿using System;

namespace ForgeCraft
{
    public class CmdForest : Command
    {
        public override string name { get { return "forest"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "op"; } }
        public override bool consoleUsable { get { return false; } }
        public override Permission defaultRank { get { return Permission.Guest; } }
        public CmdForest() { }

        public override void Use(Player p, string message)
        {
            Random rand = new Random();

            int minX = (int)p.pos.x - 50;
            int minZ = (int)p.pos.z - 50;

            for (int i = 0; i < 20; i++)
            {
                int xc = rand.Next(minX, minX + 100);
                int zc = rand.Next(minZ, minZ + 100);

                for (byte y = 100; y > 20; y--)
                {
                    ChunkBlock cb = p.currentWorld.GetBlock(xc, y, zc);

                    if ((Block)cb.type == Block.Grass)
                    {
                        AddTree(p, xc, (byte)(y + 1), zc);
                        break;
                    }
                }
            }
        }

        void AddTree(Player p, int x, byte y, int z)
        {
            ChunkBlock TreeLog = new ChunkBlock(Block.Log, 0, 0x00);
            ChunkBlock TreeLeaf = new ChunkBlock(Block.Leaves, 0, 0x00);

            Random Rand = new Random();

            byte height = (byte)Rand.Next(5, 8);

            short top = (short)(height - Rand.Next(2, 4));

            for (short xx = (short)-top; xx <= top; ++xx)
            {
                for (short yy = (short)-top; yy <= top; ++yy)
                {
                    for (short zz = (short)-top; zz <= top; ++zz)
                    {
                        short Dist = (short)(Math.Sqrt(xx * xx + yy * yy + zz * zz));
                        if (Dist < top + 1)
                        {
                            if (Rand.Next((int)(Dist)) < 2)
                            {
                                try
                                {
                                    p.currentWorld.ChangeBlock(TreeLeaf, x + xx, (byte)(y + yy + height), z + zz, p);                                    
                                }
                                catch { }
                            }
                        }
                    }
                }
            }

            for (ushort yy = 0; yy < height; yy++)
            {
                p.currentWorld.ChangeBlock(TreeLog, x, (byte)(y + yy), z, p);
            }

            
        }
        void AddCactus(Player p)
        {
            Random Rand = new Random();

            byte height = (byte)Rand.Next(3, 6);
            ushort yy;

            for (yy = 0; yy <= height; yy++)
            {
                p.currentWorld.ChangeBlock(new ChunkBlock(Block.Cactus, 0, 0x00), (int)(p.pos.x + 1), (byte)(p.pos.y + yy + height), (int)p.pos.z, p);      
            }

            int inX = 0, inZ = 0;

            switch (Rand.Next(1, 3))
            {
                case 1: inX = -1; break;
                case 2:
                default: inZ = -1; break;
            }

            p.currentWorld.ChangeBlock(new ChunkBlock(Block.Cactus, 0, 0x00), (int)(p.pos.x + inX), (byte)(p.pos.y + yy + height), (int)p.pos.z + inZ, p);

            for (yy = height; yy <= Rand.Next(height + 2, height + 5); yy++) 
            {
                p.currentWorld.ChangeBlock(new ChunkBlock(Block.Cactus, 0, 0x00), (int)(p.pos.x + 1 + inX), (byte)(p.pos.y + yy + height), (int)p.pos.z + inZ, p);
            }
            for (yy = height; yy <= Rand.Next(height + 2, height + 5); yy++)
            {
                p.currentWorld.ChangeBlock(new ChunkBlock(Block.Cactus, 0, 0x00), (int)(p.pos.x + 1 - inX), (byte)(p.pos.y + yy), (int)p.pos.z - inZ, p);
            }
        }

        public override void Help(Player p)
        {
            p.Message("/tree [type] - Turns tree mode on or off.");
            p.Message("Types - 0 - Default, 1 - Pine, 2 - Birch, 5 - Cactus)");
        }
    }
}